package hos.net.soap.convert

import com.drake.net.convert.JSONConvert
import org.json.JSONObject
import java.lang.reflect.Type

/**
 * <p>Title: JsonDataConvert </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2024-04-09 20:26
 * @version : 1.0
 */
abstract class JsonDataConvert(
    success: String = "0",
    code: String = "code",
    message: String = "msg", val data: String = "data"
) :
    JSONConvert(success, code, message) {
    override fun <R> String.parseBody(succeed: Type): R? {
        val json = JSONObject(this) // 获取JSON中后端定义的错误码和错误信息
        return json.getString(data).parseData<R>(succeed)
    }


    /**
     * 反序列化JSON
     *
     * @param succeed JSON对象的类型
     * @receiver Data字符串
     */
    abstract fun <R> String.parseData(succeed: Type): R?
}